package com.ailanyin.modules.search.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.ailanyin.common.base.constants.CommonConstant;
import com.ailanyin.common.base.exception.Assert;
import com.ailanyin.modules.search.domain.IkDict;
import com.ailanyin.modules.search.mapper.IkDictMapper;
import com.ailanyin.modules.search.service.IkDictService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * IK拓展分词Service业务层处理
 *
 * @author ailanyin
 * @date 2022-10-10
 */
@Service
public class IkDictServiceImpl implements IkDictService {

    /**
     * ik拓展热更新词典在本地的位置 默认是/file/ik_dict.txt
     * 如果需要修改别的位置/file/ik/ik_dict.txt 将IK_DICT_FILE修改成ik/ik_dict.txt即可
     * 配置ik/config/IKAnalyzer.cfg.xml<entry key="remote_ext_dict">http://localhost:1616/file/ik_dict.txt</entry>
     */
    private static final String IK_DICT_FILE = "ik_dict.txt";

    @Value("${ailanyin.uploadPath}")
    private String path;

    @PostConstruct
    public void initListToFile() throws Exception {
        File file = new File(path + IK_DICT_FILE);
        if (!file.exists()) {
            if (!file.createNewFile()) {
                Assert.fail("创建IK拓展词库失败!");
            }
        }

        addDictSetToFile();
    }

    @Resource
    private IkDictMapper ikDictMapper;

    /**
     * 查询IK拓展分词
     *
     * @param id IK拓展分词主键
     * @return IK拓展分词
     */
    @Override
    public IkDict selectIkDictById(Long id) {
        return ikDictMapper.selectIkDictById(id);
    }

    /**
     * 查询IK拓展分词列表
     *
     * @param ikDict IK拓展分词
     * @return IK拓展分词
     */
    @Override
    public List<IkDict> selectIkDictList(IkDict ikDict) {
        return ikDictMapper.selectIkDictList(ikDict);
    }

    /**
     * 新增IK拓展分词
     *
     * @param ikDict IK拓展分词
     * @return 结果
     */
    @Override
    public int insertIkDict(IkDict ikDict) {
        ikDict.setCreateTime(new Date());
        return ikDictMapper.insertIkDict(ikDict);
    }

    /**
     * 修改IK拓展分词
     *
     * @param ikDict IK拓展分词
     * @return 结果
     */
    @Override
    public int updateIkDict(IkDict ikDict) {
        return ikDictMapper.updateIkDict(ikDict);
    }

    /**
     * 批量删除IK拓展分词
     *
     * @param ids 需要删除的IK拓展分词主键
     * @return 结果
     */
    @Override
    public int deleteIkDictByIds(List<Long> ids) {
        return ikDictMapper.deleteIkDictByIds(ids);
    }

    /**
     * 删除IK拓展分词信息
     *
     * @param id IK拓展分词主键
     * @return 结果
     */
    @Override
    public int deleteIkDictById(Long id) {
        return ikDictMapper.deleteIkDictById(id);
    }

    @Override
    public void addDictToFile(IkDict ikDict) {
        if (!CommonConstant.NORMAL_STATUS.equals(ikDict.getStatus())
                || StrUtil.isBlank(ikDict.getContent())) {
            return;
        }
        File file = new File(path + IK_DICT_FILE);
        List<String> list = new ArrayList<>();
        list.add(ikDict.getContent());
        FileUtil.writeLines(list, file, "UTF-8" , true);
    }

    @Override
    public void addDictSetToFile() {
        File file = new File(path + IK_DICT_FILE);
        Set<String> dictSet = getAllAllowIkDict();
        FileUtil.writeLines(dictSet, file, "UTF-8");
    }

    private Set<String> getAllAllowIkDict() {
        return ikDictMapper.getAllAllowIkDict();
    }
}
